#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <memory>
#include <cstring>
#include "Log.hpp"

// const uint16_t defaultport = 8080;
const std::string defaultip = "127.0.0.1";
const int backlog = 5;

enum 
{
    SOCKERR = 1,
    BINDERR,
    LISTENERR
};

class Sock
{
public:
    Sock()
    {}
    void Socket()
    {
        _sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if(_sockfd < 0)
        {
            lg(FATAL, "create scoket fail... _Listensockfd: %d, errno: %d, strerror: %s", _sockfd, errno, strerror(errno));
            exit(SOCKERR);
        }
    }

    void Bind(const uint16_t &port)
    {
        struct sockaddr_in Server;
        //bzero(&Server, sizeof(Server));
        memset(&Server, 0, sizeof(Server));
        Server.sin_family = AF_INET;
        // inet_pton(AF_INET, _ip.c_str(), &Server.sin_addr);
        Server.sin_port = htons(port);
        Server.sin_addr.s_addr = INADDR_ANY;
        socklen_t len = sizeof(Server);

        int n = bind(_sockfd, (struct sockaddr *)&Server, len);
        if(n < 0)
        {
            lg(FATAL, "bind fail... _sockfd: %d, errno: %d, strerror: %s", _sockfd, errno, strerror(errno));
            exit(BINDERR);
        }
    }

    void Listen()
    {
        int n = listen(_sockfd, backlog);
        if(n < 0)
        {
            lg(FATAL, "listen fail... _sockfd: %d, errno: %d, strerror: %s", _sockfd, errno, strerror(errno));
            exit(LISTENERR);
        }
    }

    int Accept(uint16_t *clientport, std::string *clientip)
    {
        struct sockaddr_in Client;
        socklen_t len = sizeof(Client);
        int newfd = accept(_sockfd, (struct sockaddr *)&Client, &len);
        if(newfd < 0)
        {
            lg(WARNING, "accept fail... _sockfd: %d, errno: %d, strerror: %s", _sockfd, errno, strerror(errno));
        }

        *clientport = ntohs(Client.sin_port);
        char ip_str[32];
        inet_ntop(AF_INET, &Client.sin_addr, ip_str, sizeof(ip_str));
        *clientip = ip_str;

        return newfd; //返回提供服务的套接字
    }

    bool Connect(const uint16_t &port, const std::string &ip)
    {
        //告诉客户端你连接的那台主机
        struct sockaddr_in peer;
        memset(&peer, 0, sizeof(peer));
        peer.sin_family = AF_INET;
        // peer.sin_addr.s_addr = inet_addr(ip.c_str());
        //字符串转整型
        inet_pton(AF_INET, ip.c_str(), &peer.sin_addr);
        peer.sin_port = htons(port);
        socklen_t len = sizeof(peer);

        int n = connect(_sockfd, (const sockaddr *)&peer, len);
        if(n < 0)
        {
            std::cout << "connect fail... _sockfd: " << _sockfd << " ip: " << ip << " port: " << port << std::endl;
            return false;
        }

        return true;
    }

    int fd()
    {
        return _sockfd;
    }

    void Close()
    {
        close(_sockfd);
    }

    ~Sock()
    {
        if(_sockfd > 0) close(_sockfd);
    }
private:
    int _sockfd;
};